(function() {
	//轨迹播放器
	function Player(options) {
		this.trackPointsMap = new TrackPointsMap(options);
		this.initPlayer();
	}
	Player.prototype = {
		isPlaying: false, //是否正在播放
		isDone: false, //播放是否完成
		isHasData: false, //是否有数据
		currProgress: 0, //当前进度
		total: 0, //播放总数
		time: 180, //移动一次的时间，单位ms，1倍速为180
		step: 1, //步长
		timeout: null, //定时器
		pointList: [], //轨迹数据
		initPlayer: function() {
			var that = this;
			//播放、暂停按钮
			$("#play").on("tap", function() {
				if (!that.isHasData) return;
				if (that.isDone) {
					$("#play").removeClass("play").addClass("pause");
					that.isPlaying = true;
					that.isDone = false;
					that.currProgress = 0;
					that.play();
				} else {
					if (that.isPlaying) {
						that.isPlaying = false;
						that.pause();
						$("#play").removeClass("pause").addClass("play");
					} else {
						that.isPlaying = true;
						that.play();
						$("#play").removeClass("play").addClass("pause");
					}
				}
			});
			//停止按钮
			$("#stop").on("tap", function() {
				if (!that.isHasData) return;
				that.stop();
			});
			//播放速度按钮
			$("#onex").on("tap", function() {
				if (!that.isHasData) return;
				that.time = 180;
				$("#onex").removeClass("onex").addClass("onex_disable");
				$("#twox").removeClass("twox_disable").addClass("twox");
				$("#threex").removeClass("threex_disable").addClass("threex");
			});
			$("#twox").on("tap", function() {
				if (!that.isHasData) return;
				that.time = 120;
				$("#twox").removeClass("twox").addClass("twox_disable");
				$("#onex").removeClass("onex_disable").addClass("onex");
				$("#threex").removeClass("threex_disable").addClass("threex");
			});
			$("#threex").on("tap", function() {
				if (!that.isHasData) return;
				that.time = 60;
				$("#threex").removeClass("threex").addClass("threex_disable");
				$("#onex").removeClass("onex_disable").addClass("onex");
				$("#twox").removeClass("twox_disable").addClass("twox");
			});
			//前进、后退按钮
			$("#next").on("tap", function() {
				if (!that.isHasData || that.isPlaying || that.currProgress == that.total) return;
				that.trackPointsMap.setMarkerOnMap(true);
				that.currProgress += 1;
				$("#previous").removeClass("prev_disable").addClass("prev");
				if (that.currProgress == that.total)
					$("#next").removeClass("next").addClass("next_disable");
				$("#progress").val(that.currProgress);
				that.updateCurrentPosition();
			});
			$("#previous").on("tap", function() {
				if (!that.isHasData || that.isPlaying || that.currProgress == 0) return;
				$("#next").removeClass("next_disable").addClass("next");
				that.currProgress--;
				if (that.currProgress == 0) {
					that.trackPointsMap.setMarkerOnMap(false);
					$("#previous").removeClass("prev").addClass("prev_disable");
					$("#vehiclecontent").html("");
				} else
					that.updateCurrentPosition();
				$("#progress").val(that.currProgress);
			});
			//监听滑块input事件
			document.querySelector('input[type="range"]').addEventListener("input", function() {
				if (!that.isHasData) {
					$("#progress").val(0);
					return;
				}
				that.currProgress = this.value * 1;
				if (that.currProgress == that.total)
					$("#next").removeClass("next").addClass("next_disable");
				else
					$("#next").removeClass("next_disable").addClass("next");
				if (that.currProgress == 0) {
					//					marker.setMap(null);
					$("#previous").removeClass("prev").addClass("prev_disable");
					$("#vehiclecontent").html("");
				} else {
					that.trackPointsMap.setMarkerOnMap(true);
					$("#previous").removeClass("prev_disable").addClass("prev");
					that.updateCurrentPosition();
				}
			});
		},
		play: function() {
			var that = this;
			if (that.currProgress == that.total)
				$("#next").removeClass("next").addClass("next_disable");
			else
				$("#next").removeClass("next_disable").addClass("next");
			if (that.currProgress == 0)
				$("#previous").removeClass("prev").addClass("prev_disable");
			else
				$("#previous").removeClass("prev_disable").addClass("prev");
			$("#progress").val(that.currProgress);
			if (that.currProgress == 0) {
				$("#vehiclecontent").html("");
				that.trackPointsMap.setMarkerOnMap(false);
				that.trackPointsMap.setMarkerPosition(that.pointList[0].lng, that.pointList[0].lat);
			} else {
				that.trackPointsMap.setMarkerOnMap(true);
				that.updateCurrentPosition();
			}
			if (that.currProgress == that.total) {
				that.isPlaying = false;
				that.isDone = true;
				$("#play").removeClass("pause").addClass("play");
			} else
				that.currProgress += 1;
			if (!that.isDone)
				that.timeout = setTimeout(function() {
					that.play();
				}, that.time);
		},
		pause: function() {
			clearTimeout(this.timeout);
		},
		stop: function() {
			var that = this;
			that.trackPointsMap.setMarkerPosition(that.pointList[0].lng, that.pointList[0].lat);
			$("#progress").val(0);
			$("#vehiclecontent").html("");
			clearTimeout(that.timeout);
			that.currProgress = 0;
			that.isDone = false;
			that.isPlaying = false;
			$("#play").removeClass("pause").addClass("play");
			$("#next").removeClass("next_disable").addClass("next");
			$("#previous").removeClass("prev").addClass("prev_disable");
		},
		updateCurrentPosition: function() {
			this.trackPointsMap.setMarkerPosition(this.pointList[this.currProgress - 1].lng, this.pointList[this.currProgress - 1].lat);
			$("#vehiclecontent").html("<span>" + this.pointList[this.currProgress - 1].gpsTime + "</span><span>" + degreeDirect(this.pointList[this.currProgress - 1].direction) + "</span><span>" + this.pointList[this.currProgress - 1].speed + "KM/H</span>");
		},
		renew: function() {
			$("#progress").val(0);
			$("#vehiclecontent").html("");
			this.pointList = [];
			this.total = 0;
			this.isHasData = false;
			this.currProgress = 0;
			this.isDone = false;
			this.trackPointsMap.clearMap();
			$("#play").removeClass("pause").addClass("play");
			$("#next").removeClass("next_disable").addClass("next");
			$("#previous").removeClass("prev").addClass("prev_disable");
		},
		setData: function(list) {
			this.pointList = list;
			this.total = list.length;
			console.log(list.length);
			$("#progress").attr("max", list.length);
			this.isHasData = true;
			this.trackPointsMap.draw(list);
		}
	};
	//轨迹地图
	function TrackPointsMap(options) {
		var defaultSetting = {
			mapId: null,
			mapOption: {
				view: new AMap.View2D({
					zoom: 17
				})
			}
		};
		this.setting = $.extend(true, defaultSetting, options);
		this.mapFactory = new mapFactory();
		this.initMap();
	}
	TrackPointsMap.prototype = {
		mapObj: null,
		marker: null,
		//初始化地图
		initMap: function() {
			this.mapObj = this.mapFactory.newMap(this.setting.mapId, this.setting.mapOption);
			this.mapFactory.plugin(this.mapObj, ["AMap.ToolBar", "AMap.OverView"]);
		},
		draw: function(list) { //绘制marker和polyline
			var that = this;
			that.marker = that.mapFactory.newMarker({
				map: that.mapObj,
				position: that.mapFactory.newLngLat(list[0].lng, list[0].lat),
				content: '<div class="monitor_marker"></div>',
				offset: that.mapFactory.newPixel(-10, -26),
				autoRotation: true
			});
			var lineArr = new Array();
			for (var v in list)
			{
				//console.log(list[v].lng);
				lineArr.push(that.mapFactory.newLngLat(list[v].lng, list[v].lat));
			}
			var polyline = that.mapFactory.Polyline({
				map: that.mapObj,
				path: lineArr
			});
			that.mapObj.setFitView();
		},
		setMarkerOnMap: function(flag) {
			if (flag)
				this.marker.setMap(this.mapObj);
			else
				this.marker.setMap(null);
		},
		setMarkerPosition: function(lng, lat) {
			var that = this;
			that.marker.setPosition(that.mapFactory.newLngLat(lng, lat));
		},
		clearMap: function() {
			this.mapObj.clearMap();
		}
	};
	//页面加载
	var page = {
		player: null,
		sendData: {
			vehId: "",
			timeBegin: "",
			timeEnd: ""
		},
		init: function() {
			mui.init();
			this.player = new Player({
				mapId: "trackpointsmap"
			});
			$("#trackpointsmap").height($(window).height() - $("#trackpointsinfo").height() - $("#player").height());
			var request = tiza.getRequest();
			this.sendData.vehId = request.vehId;
			//console.log("guiji"+request.vehId)
			this.onTap();
		},
		onTap: function() {
			var that = this;
			//默认当前日期

			var timeBegin = document.getElementById("txtBegin");
			var timeEnd = document.getElementById("txtEnd") ;
			var d = new Date();
			timeBegin.value = addDate(d, -1);
			//console.log()
			timeEnd.value = addDate(d, 0);
			var minDate = new Date();
			minDate.setFullYear(2000, 0, 1);
			var maxDate = new Date();
			maxDate.setFullYear(2050, 12, 31);
			//弹出日期选择
			mui(".mui-content").on("tap", "#txtBegin", function() {
				plus.nativeUI.pickDate(function(e) {
					var d = e.date;
					document.getElementById("txtBegin").value = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate()
				}, function(e) {
					mui.toast("您没有选择日期");
				}, {
					title: "请选择日期",
					date:new Date(document.getElementById("txtBegin").value)
				});
			}, false);

			//弹出日期选择
			mui(".mui-content").on("tap", "#txtEnd", function() {
				plus.nativeUI.pickDate(function(e) {
					var d = e.date;
					var timeBegin = document.getElementById("txtBegin");
					var timeEnd = document.getElementById("txtEnd") ;
					timeEnd.value = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
					
				}, function(e) {
					mui.toast("您没有选择日期");
				}, {
					title: "请选择日期",
					date:new Date(document.getElementById("txtEnd").value)
				});
			}, false);

			//轨迹查询
			$("#confirm").on("tap", function() {
				if (that.player.isPlaying) return;
				plus.nativeUI.showWaiting();
				that.player.renew();
				var timeBegin = document.getElementById("txtBegin");
				var timeEnd = document.getElementById("txtEnd") ;
				var sta_date = new Date(timeBegin.value);
				var end_date = new Date(timeEnd.value);
				var days=Math.floor((end_date-sta_date)/(24*3600*1000));
				
				if(timeEnd.value != "" && timeBegin.value != ""){
					if(days <0) {
						plus.nativeUI.closeWaiting();
						mui.toast("请选择正确的开始和结束日期");
						return;
					}
					if(days <= 7){
						that.sendData.timeBegin = document.getElementById("txtBegin").value + " 00:00:00";
						that.sendData.timeEnd = document.getElementById("txtEnd").value  + " 23:59:59";
						console.log(that.sendData.timeBegin);
			           //c测试
//			           var ret= "{\"state\":\"001\",\"msg\":null,\"data\":[{\"gpsTime\":\"2016-07-11 08:11:42\",\"position\":\"西藏 拉萨 墨竹工卡县\",\"lat\":\"29.367911\",\"lng\":\"91.515914\",\"speed\":\"0\",\"direction\":\"0\"},{\"gpsTime\":\"2016-07-11 08:19:42\",\"position\":\"西藏 拉萨 墨竹工卡县\",\"lat\":\"29.397911\",\"lng\":\"90.515914\",\"speed\":\"0\",\"direction\":\"0\"}]}";
//			            var result=JSON.parse(ret);
//			            console.log(ret);
//			            console.log(result);
//			            that.player.setData(result.data);
//						plus.nativeUI.closeWaiting();
						//c测试
						tiza.ajax("Veh/Trace", {
							data: that.sendData,
							type: "get",
							success: function(result) {
								console.log(JSON.stringify(result.data));
								if (result.data.length == 0)
									plus.nativeUI.toast("无历史记录");
								else
								{
									 
									that.player.setData(result.data);
								}
								plus.nativeUI.closeWaiting();
							}
						});
						document.activeElement.blur();
					}else{
						plus.nativeUI.closeWaiting();
						mui.toast("请查询7天内的数据");
					}
				}else{
					plus.nativeUI.closeWaiting();
					mui.toast("请选择开始和结束日期");
				}
				
				

			});
		}
	};
	if (window.plus)
		page.init();
	else
		document.addEventListener("plusready", function() {
			page.init();
		}, false);
})();